package com.shujia.flink.tf

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.streaming.api.windowing.windows.TimeWindow

object Demo4Window {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)

    /**
     * 统计最近10单词的数量
     */
    val wordsDS: DataStream[String] = linesDS.flatMap(_.split(","))
    val kvDS: DataStream[(String, Int)] = wordsDS.map((_, 1))

    val keyByDS: KeyedStream[(String, Int), String] = kvDS.keyBy(_._1)

    /**
     * window: 串钩操作，类型每隔一段时间计算一次的需求需要使用窗口计算
     *
     * TumblingProcessingTimeWindowsL 处理时间的滚动窗口
     *
     */
    //每隔10秒计算一次
    val windowDS: WindowedStream[(String, Int), String, TimeWindow] = keyByDS
      .window(TumblingProcessingTimeWindows.of(Time.seconds(10)))

    //窗口内聚合计算
    val countDS: DataStream[(String, Int)] = windowDS.sum(1)


    countDS.print()

    env.execute()
  }

}
